﻿Imports CupsApp.Data
Imports CupsApp.Service

Partial Public Class manage_properties
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        CType(Master.FindControl("lnkNewLandlords"), LinkButton).Enabled = False
        CType(Master.FindControl("lnkNewLandlords"), LinkButton).Text = "> Landlords <"

        lvlMsg.Text = ""

    End Sub


    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click

        lvlMsg.Text = ""
        If btnSave.Text = "Save" Then
            SaveProperties()
        Else
            UpdateProperties()
        End If

        ClearField()

    End Sub



    Public Sub UpdateProperties()

        Dim cds As New CupsDomainService()
        Dim objProperties As New cups_PropertyLandlord
        Dim Address As String = ""
        Dim City As String = ""
        Dim PostalCode As String = ""
        Dim Suite As String = ""
        Dim Status As String = ""

        Try

            Address = txtAddress.Text.Trim
            City = ddlCity.SelectedItem.Text
            PostalCode = txtPostal.Text.Trim
            Suite = txtSuite.Text.Trim
            Status = ddlStatus.SelectedItem.Value


            With objProperties
                .LandlordID = Request.QueryString("LandlordID")
                .Address = Address
                .City = City
                .PostalCode = PostalCode
                .Suite = Suite
                .CurrentStatusID = Status
                .UpdatedBy = cds.GetCurrentUser
                .UpdatedOn = Date.Now()
            End With

            cds.UpdateProperty(objProperties, ViewState("PropertyID"))

            gvProperties.DataBind()

            btnBack.Text = "Back"
            btnSave.Text = "Save"
            gvProperties.Enabled = True

            lvlMsg.Text = "* Property information was updated successfully!"

        Catch ex As Exception
            lvlMsg.ForeColor = Drawing.Color.Red
            lvlMsg.Text = "* Error: Property information can not be updated!"
        End Try

    End Sub

    Public Sub SaveProperties()

        Dim cds As New CupsDomainService()
        Dim objProperties As New cups_PropertyLandlord
        Dim Address As String = ""
        Dim City As String = ""
        Dim PostalCode As String = ""
        Dim Suite As String = ""
        Dim Status As String = ""

        Try

            Address = txtAddress.Text.Trim
            City = ddlCity.SelectedItem.Text
            PostalCode = txtPostal.Text.Trim
            Suite = txtSuite.Text.Trim
            Status = ddlStatus.SelectedItem.Value


            With objProperties
                .LandlordID = Request.QueryString("LandlordID")
                .Address = Address
                .City = City
                .PostalCode = PostalCode
                .Suite = Suite
                .CurrentStatusID = Status
                .UpdatedBy = cds.GetCurrentUser
                .UpdatedOn = Date.Now()
            End With

            cds.SaveLandlordProperties(objProperties)

            gvProperties.DataBind()
            lvlMsg.Text = "* Property information was save successfully!"
        Catch ex As Exception
            lvlMsg.ForeColor = Drawing.Color.Red
            lvlMsg.Text = "* Error: Property information can not be saved!"
        End Try

    End Sub

    Public Sub ClearField()

        txtAddress.Text = ""
        ddlCity.SelectedValue = 0
        txtPostal.Text = ""
        txtSuite.Text = ""
        ddlStatus.SelectedValue = 0

    End Sub

    Protected Sub btnBack_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnBack.Click

        If btnBack.Text = "Cancel" Then
            btnBack.Text = "Back"
            btnSave.Text = "Save"
            gvProperties.Enabled = True
        Else
            Response.Redirect("manage_landlords.aspx")
        End If

        lvlMsg.Text = ""
        ClearField()

    End Sub

    Protected Sub gvProperties_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles gvProperties.RowCommand
        Dim cds As New CupsDomainService()
        Dim rowIndex As Integer = Integer.Parse(e.CommandArgument.ToString())
        Dim PropertyID As Integer = Convert.ToInt64(gvProperties.Rows(rowIndex).Cells(1).Text)

        If e.CommandName = "Modify" Then

            btnSave.Text = "Update"
            btnBack.Text = "Cancel"
            ViewState("PropertyID") = PropertyID
            FillValue(PropertyID)

            gvProperties.Enabled = False
        ElseIf e.CommandName = "Clients" Then

            If cds.ClientPropertyExist(PropertyID) Then
                Response.Redirect("~/Admin/client_property.aspx?PropertyID=" & PropertyID & "&LandlordID=" & Request.QueryString("LandlordID") & "")
            Else
                lvlMsg.ForeColor = Drawing.Color.Red
                lvlMsg.Text = "* Property has no clients attached!"
            End If

        End If

    End Sub

    Public Sub FillValue(ByVal PropertyID As Integer)

        Dim cds As New CupsDomainService()
        Dim objProperties As New cups_PropertyLandlord

        objProperties = cds.GetPropertyForEdit(PropertyID)

        With objProperties
            txtAddress.Text = .Address
            ddlCity.SelectedValue = 1
            txtPostal.Text = .PostalCode
            txtSuite.Text = .Suite
            ddlStatus.SelectedValue = 1
        End With

    End Sub
End Class